package com.LeeCode;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合
 */

public class Code077 {
    public static void main(String[] args) {
        int n = 4, k = 2;
        System.out.println(new Code077().combine(n, k));
    }

    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ans = new ArrayList<>();
        backtrace(1, n, k, new ArrayList<>(), ans);
        return ans;
    }

    private void backtrace(int index, int n, int k, List<Integer> cur, List<List<Integer>> ans) {
        if (k == 0) {
            ans.add(new ArrayList<>(cur));
            return;
        }
        if (index > n) return;

        for (int i = index; i <= n - k + 1; i++) {
            cur.add(i);
            backtrace(i + 1, n, k - 1, cur, ans);
            cur.remove(cur.size() - 1);
        }
    }
}
